﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Reflection;
using YWM.Common.Extensions;
using YWM.Repositories.Extensions;

namespace YWM.EntityFramework
{
    public class EntityFrameworkModule : BaseModule
    {
        public override void Initialize()
        {
            List<Type> dbContextTypes = AppDomain.CurrentDomain.GetTypes(type =>
            {
                TypeInfo typeInfo = type.GetTypeInfo();
                return typeInfo.IsPublic &&
                       !typeInfo.IsAbstract &&
                       typeInfo.IsClass &&
                       typeof(EFDbContext).IsAssignableFrom(type);
            });
            foreach (Type dbContextType in dbContextTypes)
            {
                ContainerBuilder.RegisterRepositories(dbContextType, typeof(DbSet<>));
            }
            ContainerBuilder.RegisterAssemblyType(GetType().Assembly);
        }
    }
}